package com.cykj.mqConsumer;

import com.cykj.entity.CyOrderDO;
import com.cykj.feign.ProDuctFeignClient;
import com.cykj.mapper.OrderMapper;
import com.cykj.mqEvent.CancelOrderEvent;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Project:JDMS
 * Package:com.cykj.mqConsumer
 *
 * @Description:
 * @Author:其一
 * @CreateTime: 2025-07-31-16-19
 */
@Component
@RocketMQMessageListener(topic = "cancel-order",consumerGroup = "ssh-cancel-order")
public class CancelOrderConsumer implements RocketMQListener<CancelOrderEvent> {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ProDuctFeignClient proDuctFeignClient;

    @Override
    public void onMessage(CancelOrderEvent cancelOrderEvent) {
        Integer orderId = cancelOrderEvent.getOrderId();

        CyOrderDO cyOrderDO = orderMapper.selectOneById(orderId);
        if(cyOrderDO.getPayState()== 0 ){
            redisTemplate.opsForValue().increment("goodStock:"+cyOrderDO.getGoodId(),cyOrderDO.getBuyCount());
            orderMapper.updateOrderTimeOut(cyOrderDO);
        }else {
            Integer goodId = cyOrderDO.getGoodId();
            Integer buyCount = cyOrderDO.getBuyCount();
            proDuctFeignClient.updateGoodStock(buyCount,goodId);
        }
    }
}
